#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<assert.h>
using namespace std;
#include"list.h"


void test_list1()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	lt.push_front(10);
	lt.push_front(20);
	lt.push_front(30);
	lt.push_front(40);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	lt.pop_back();
	lt.pop_back();
	lt.pop_front();
	lt.pop_front();

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	auto pos = find(lt.begin(), lt.end(), 3);

	if (pos != lt.end())
	{
		//insert之后pos是否失效呢？不失效,因为链表不存在挪动空间和扩容的问题
		lt.insert(pos, 30);
	}
	cout << *pos << endl;
	//(*pos)++;
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	lt.erase(pos);//erase之后，pos就失效了，因为空间都被干掉了
	//cout << *pos << endl;
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

}

//Operations:
void test_list3()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(5);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(4);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	lt.push_back(7);
	lt.push_back(6);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	lt.remove(3);
	lt.remove(30);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	lt.sort();
	//迭代器·功能分类
	//1、单向 ++ 
	//2、双向 ++ -- list
	//3、随机 + -
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	//去重：建立在有序的基础上
	lt.unique();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

}

void test_op()
{
	srand(time(0));
	const int N = 10000000;
	vector<int> v;
	v.reserve(N);

	list<int> lt1;
	list<int> lt2;
	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		//v.push_back(e);
		lt1.push_back(e);
		lt2.push_back(e);
	}

	// 拷贝到vector排序，排完以后再拷贝回来
	int begin1 = clock();
	for (auto e : lt1)
	{
		v.push_back(e);
	}
	sort(v.begin(), v.end());
	size_t i = 0;
	for (auto& e : lt1)
	{
		e = v[i++];
	}
	int end1 = clock();

	int begin2 = clock();
	// sort(lt.begin(), lt.end());
	lt2.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}


int main()
{
	//test_list3();
	//test_op();

	cfy::test_list6();
	//突发奇想：但是这样是不对的
	//int* p = (int*)8;
	//cout << p << endl;
	//int  ptr = 1;
	//cout << &ptr << endl;
	//int* p = (int*)0x010ffe60;//加了个0x
	//p = 1;
	//cout << *p;

	//string s1("1111");
	//string s2("2222222222222222222222222222222222222222222");

	//1. 大小一样
	//2. 大小计算：12？ vs2013-> 28  编译器做了优化，加了一个16字节的buff
	// char buff[16]
	// char* ptr
	// size_t _size
	//size_t _capacity
	//cout << sizeof(s1) << endl;
	//cout << sizeof(s2) << endl;
	return 0;
}

